package GUI;

import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import Arbori.ArboreFunctional;
import Visitor.ToStringVisitor;
import Visitor.Visitor;
import XML.SaveXML;

import com.cloudgarden.layout.AnchorConstraint;
import com.cloudgarden.layout.AnchorLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JTextField;

import javax.swing.WindowConstants;
import javax.swing.SwingUtilities;

/**
 * This code was edited or generated using CloudGarden's Jigloo SWT/Swing GUI
 * Builder, which is free for non-commercial use. If Jigloo is being used
 * commercially (ie, by a corporation, company or business for any purpose
 * whatever) then you should purchase a license for each developer using Jigloo.
 * Please visit www.cloudgarden.com for details. Use of Jigloo implies
 * acceptance of these licensing terms. A COMMERCIAL LICENSE HAS NOT BEEN
 * PURCHASED FOR THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED LEGALLY FOR
 * ANY CORPORATE OR COMMERCIAL PURPOSE.
 */
public class Main extends javax.swing.JFrame implements ActionListener {
	private JLabel jLabel1;
	private JLabel jLabel2;
	private JLabel jLabel3;
	private JLabel jLabel4;
	private JLabel jLabel5;
	private JButton jButton1;
	private JLabel error;
	private JTextField fdx0;
	private JTextField fd;
	private JTextField fx0;
	private JTextField x0;
	private JTextField fx;
	ArboreFunctional a = null;
	SaveXML doc;
	public double xz = 0;
	public String functie;

	/**
	 * Auto-generated main method to display this JFrame
	 */
	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				Main inst = new Main("Functie");
				inst.setLocationRelativeTo(null);
				inst.setVisible(true);
			}
		});
	}

	public Main(String nume) {
		super(nume);
		initGUI();
	}

	private void initGUI() {
		try {
			AnchorLayout thisLayout = new AnchorLayout();
			getContentPane().setLayout(thisLayout);
			setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
			{
				error = new JLabel();
				getContentPane().add(
						error,
						new AnchorConstraint(856, 974, 1004, 27,
								AnchorConstraint.ANCHOR_REL,
								AnchorConstraint.ANCHOR_REL,
								AnchorConstraint.ANCHOR_REL,
								AnchorConstraint.ANCHOR_REL));
				error.setPreferredSize(new java.awt.Dimension(538, 15));
			}
			{
				jButton1 = new JButton();
				getContentPane().add(
						jButton1,
						new AnchorConstraint(599, 453, 806, 27,
								AnchorConstraint.ANCHOR_REL,
								AnchorConstraint.ANCHOR_REL,
								AnchorConstraint.ANCHOR_REL,
								AnchorConstraint.ANCHOR_REL));
				jButton1.setText("Calculeaza");
				jButton1.setPreferredSize(new java.awt.Dimension(191, 21));
				jButton1.addActionListener(this);
			}
			{
				fdx0 = new JTextField();
				getContentPane().add(
						fdx0,
						new AnchorConstraint(569, 974, 777, 580,
								AnchorConstraint.ANCHOR_REL,
								AnchorConstraint.ANCHOR_REL,
								AnchorConstraint.ANCHOR_REL,
								AnchorConstraint.ANCHOR_REL));
				fdx0.setPreferredSize(new java.awt.Dimension(177, 21));
				fdx0.setEditable(false);
			}
			{
				fd = new JTextField();
				getContentPane().add(
						fd,
						new AnchorConstraint(311, 974, 519, 580,
								AnchorConstraint.ANCHOR_REL,
								AnchorConstraint.ANCHOR_REL,
								AnchorConstraint.ANCHOR_REL,
								AnchorConstraint.ANCHOR_REL));
				fd.setPreferredSize(new java.awt.Dimension(177, 21));
				fd.setEditable(false);
			}
			{
				fx0 = new JTextField();
				getContentPane().add(
						fx0,
						new AnchorConstraint(74, 974, 282, 580,
								AnchorConstraint.ANCHOR_REL,
								AnchorConstraint.ANCHOR_REL,
								AnchorConstraint.ANCHOR_REL,
								AnchorConstraint.ANCHOR_REL));
				fx0.setPreferredSize(new java.awt.Dimension(177, 21));
				fx0.setEditable(false);
			}
			{
				jLabel5 = new JLabel();
				getContentPane().add(
						jLabel5,
						new AnchorConstraint(599, 553, 737, 477,
								AnchorConstraint.ANCHOR_REL,
								AnchorConstraint.ANCHOR_REL,
								AnchorConstraint.ANCHOR_REL,
								AnchorConstraint.ANCHOR_REL));
				jLabel5.setText("f '(x0)=");
			}
			{
				jLabel4 = new JLabel();
				getContentPane().add(
						jLabel4,
						new AnchorConstraint(341, 540, 480, 477,
								AnchorConstraint.ANCHOR_REL,
								AnchorConstraint.ANCHOR_REL,
								AnchorConstraint.ANCHOR_REL,
								AnchorConstraint.ANCHOR_REL));
				jLabel4.setText("f '(x)=");
			}
			{
				jLabel3 = new JLabel();
				getContentPane().add(
						jLabel3,
						new AnchorConstraint(103, 535, 242, 477,
								AnchorConstraint.ANCHOR_REL,
								AnchorConstraint.ANCHOR_REL,
								AnchorConstraint.ANCHOR_REL,
								AnchorConstraint.ANCHOR_REL));
				jLabel3.setText("f(x0)=");
			}
			{
				x0 = new JTextField();
				getContentPane().add(
						x0,
						new AnchorConstraint(311, 451, 519, 99,
								AnchorConstraint.ANCHOR_REL,
								AnchorConstraint.ANCHOR_REL,
								AnchorConstraint.ANCHOR_REL,
								AnchorConstraint.ANCHOR_REL));
				x0.setPreferredSize(new java.awt.Dimension(158, 21));
			}
			{
				fx = new JTextField();
				getContentPane().add(
						fx,
						new AnchorConstraint(74, 451, 282, 99,
								AnchorConstraint.ANCHOR_REL,
								AnchorConstraint.ANCHOR_REL,
								AnchorConstraint.ANCHOR_REL,
								AnchorConstraint.ANCHOR_REL));
				fx.setPreferredSize(new java.awt.Dimension(158, 21));
			}
			{
				jLabel2 = new JLabel();
				getContentPane().add(
						jLabel2,
						new AnchorConstraint(341, 59, 480, 27,
								AnchorConstraint.ANCHOR_REL,
								AnchorConstraint.ANCHOR_REL,
								AnchorConstraint.ANCHOR_REL,
								AnchorConstraint.ANCHOR_REL));
				jLabel2.setText("x=");
			}
			{
				jLabel1 = new JLabel();
				getContentPane().add(
						jLabel1,
						new AnchorConstraint(74, 88, 282, 27,
								AnchorConstraint.ANCHOR_REL,
								AnchorConstraint.ANCHOR_REL,
								AnchorConstraint.ANCHOR_REL,
								AnchorConstraint.ANCHOR_REL));
				jLabel1.setText("f(x)=");
				jLabel1.setPreferredSize(new java.awt.Dimension(35, 21));
			}
			pack();
			this.setSize(576, 128);
		} catch (Exception e) {
			// add your error handling code here
			e.printStackTrace();
		}
	}

	public Main() throws HeadlessException {
		super();
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == jButton1) {
			if (fx.getText().length() != 0) {
				if (x0.getText().length() != 0) {
					jLabel3.setText("f(" + x0.getText() + ")=");
					jLabel5.setText("f '(" + x0.getText() + ")=");
					error.setText("");
					functie = fx.getText();

					Command co = new Parsare(a, functie);
					error.setVisible(true);
					a = co.execute();

//					try {
//						doc = new SaveXML(a.getRadacina(), "output.xml");
//					} catch (IOException e) {
//						System.err.println("Eroare la intrare");
					// }

					error.setVisible(true);
					error.setText(co.getErr());

					Visitor v = new ToStringVisitor();
					a.acceptVisitor(v);

					xz = Double.parseDouble(x0.getText());
					Completare c = new Completare(a, fx0, fd, fdx0, xz);
					c.completare();
				} else {
					error.setText("Dati valoare lui x !!!");
				}
			} else {
				error.setText("Va rog introduceti o functie !!!");
			}
		}

	}

}
